package com.yxhl.zoume.core.expresscar.presenter;

import android.content.Context;

import com.yxhl.protobuf.CommonRespParams;
import com.yxhl.protobuf.MpvModel;
import com.yxhl.protobuf.MpvModelRequest;
import com.yxhl.protobuf.MpvModelResponse;
import com.yxhl.protobuf.OrderBizType;
import com.yxhl.protobuf.RentalTime;
import com.yxhl.zoume.common.presenter.BasePresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.car.view.common.GetRentCarTimeView;
import com.yxhl.zoume.core.car.view.common.VehicleShowView;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.car.expresscar.QueryVehicleModelUseCase;

import java.util.List;

import javax.inject.Inject;

/**
 * Author：zhongshan   
 * Date：2016/12/07 14:51      
 * Description：单程用车／分时租车－查询车型；分时租车－查询租车时长
 */
public class QueryVehiclePresenter extends BasePresenter {
    @Inject
    QueryVehicleModelUseCase mQueryVehicleModelUseCase;

    @Inject
    public QueryVehiclePresenter(Context context) {
        super(context);
    }

    private VehicleShowView mVehicleShowView;
    private List<MpvModel> mVehicleModelsList;
    private GetRentCarTimeView mGetRentCarTimeView;

    /**
     * 查询价格和距离
     *
     * @param bizType
     */
    public void queryOfficialCarDisplayData(OrderBizType bizType) {

        MpvModelRequest request = MpvModelRequest
                .newBuilder()
                .setCommonRequest(getCommonReqParams())
                .setBindType(bizType)
                .build();

        mQueryVehicleModelUseCase.execute(request,new ZMSubscriber<MpvModelResponse>() {
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mVehicleShowView.renderVehicleModeDataOnError(e.toString());
                    }

                    @Override
                    public void onNext(MpvModelResponse mpvModelResponse) {
                        super.onNext(mpvModelResponse);
                        CommonRespParams commonResponse = mpvModelResponse.getCommonResponse();
                        if (commonResponse != null) {
                            String isSucc = commonResponse.getIsSucc();
                            if ("true".equalsIgnoreCase(isSucc)) {
                                mVehicleModelsList = mpvModelResponse.getVehicleModelsList();
                                if (mVehicleModelsList != null && mVehicleModelsList.size() >= 0) {
                                    mVehicleShowView.renderVehicleModelDataOnSuccess(mVehicleModelsList);
                                }

                                List<RentalTime> rentalTimesList = mpvModelResponse.getRentalTimesList();
                                if (rentalTimesList != null && rentalTimesList.size() >= 0) {
                                    if (mGetRentCarTimeView != null) {
                                        mGetRentCarTimeView.renderRentCarTimeOnSuccess(rentalTimesList);
                                    }
                                }

                            } else {
                                mVehicleShowView.renderVehicleModelDataOnFailure(commonResponse.getResultMsg());
                            }
                        }
                    }
                });

    }

    @Override
    public void attachView(BaseView view) {
        if (view instanceof VehicleShowView) {
            mVehicleShowView = (VehicleShowView) view;
        }

        if (view instanceof GetRentCarTimeView) {
            mGetRentCarTimeView = (GetRentCarTimeView) view;
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mQueryVehicleModelUseCase.unsubscribe();
    }
}
